﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HighLowTriple7;
using HighLowTriple7.Entity;
using Newtonsoft.Json;
using PaymentBLL;

namespace WebHiLoDemo
{
    public partial class Default : System.Web.UI.Page
    {
        private const string ACC_KEY = "ACCESS_TOKEN";
        private const int ACCOUNT_ID = 5831;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string accessToken = Session[ACC_KEY] as string;
                if (string.IsNullOrEmpty(accessToken))
                {
                    accessToken = Triple7.Instance(ACCOUNT_ID, "test", 1);
                    Session[ACC_KEY] = accessToken;
                }

                if (!IsPostBack)
                {
                    int status = Triple7.GetGameStatus(accessToken);
                    if (status == 4)
                    {
                        btnCollect.Enabled = btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = false;
                        txtBet.Enabled = btnBet.Enabled = true;
                        SetInfo(null);
                    }
                    else if (status == 5)
                    {
                        btnCollect.Enabled = (Triple7.GetRound(accessToken) > 1);
                        txtBet.Enabled = btnBet.Enabled = false;
                        btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = true;
                        SetInfo(Triple7.GetRateConfig(Triple7.GetAuCurrent(accessToken), accessToken));
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        private void SetInfo(RateConfigEntity rate)
        {
            CtrAccount ctrAccount = new CtrAccount();
            lblAuCurrent.Text = "Vàng hiện tại: " + ctrAccount.SelectAccountInfo(ACCOUNT_ID).Au.ToString();

            if (rate == null)
            {
                txtBet.Text = "100";
                lblCol1Hi.Text = "0";
                lblCol1Lo.Text = "0";
                lblCol2Hi.Text = "0";
                lblCol2Lo.Text = "0";
                lblCol3Hi.Text = "0";
                lblCol3Lo.Text = "0";
            }
            else
            {
                txtBet.Text = rate.Bet.ToString();
                lblCol1Hi.Text = rate.Column1WinForHigh.ToString();
                lblCol1Lo.Text = rate.Column1WinForLow.ToString();
                lblCol2Hi.Text = rate.Column2WinForHigh.ToString();
                lblCol2Lo.Text = rate.Column2WinForLow.ToString();
                lblCol3Hi.Text = rate.Column3WinForHigh.ToString();
                lblCol3Lo.Text = rate.Column3WinForLow.ToString();
            }

            string accessToken = Session[ACC_KEY] as string;
            Triple7SessionInfo info = Triple7.GetSessionInfo(accessToken);
            //lblJackpot.Text = string.Format("Jackpot: {0}", info.Jackpot);

            lblCol1.Text = info.Column1Value.ToString();
            lblCol2.Text = info.Column2Value.ToString();
            lblCol3.Text = info.Column3Value.ToString();

            if (info.GameStatus == 5)
            {
                btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = true;

                btnCol1Hi.Enabled = (info.Column1Value != 13);

                btnCol1Lo.Enabled = (info.Column1Value != 1);

                btnCol2Hi.Enabled = (info.Column2Value != 13);

                btnCol2Lo.Enabled = (info.Column2Value != 1);

                btnCol3Hi.Enabled = (info.Column3Value != 13);

                btnCol3Lo.Enabled = (info.Column3Value != 1);
            }

            btnNudges.Text = string.Format("Shuffle {0}", info.Nudges);
            btnNudges.Enabled = (info.Nudges > 0);
        }

        private void SelectHiLo(int column, int selectType)
        {
            string accessToken = Session[ACC_KEY] as string;

            switch (selectType)
            {
                case 1:
                    Triple7.PlayerSelectHigh(column, accessToken);
                    break;
                case 2:
                    Triple7.PlayerSelectLow(column, accessToken);
                    break;
            }

            int status = Triple7.GetGameStatus(accessToken);
            if (status == 5)
            {
                btnCollect.Enabled = true;
                int au = Triple7.GetAuCurrent(accessToken);
                txtBet.Text = au.ToString();
                SetInfo(Triple7.GetRateConfig(au, accessToken));
            }
            else if (status == 4)
            {
                btnCollect.Enabled = btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = false;
                txtBet.Enabled = btnBet.Enabled = true;
                SetInfo(null);
            }
            lblMessage.Text = "Kết quả random: " + Triple7.GetLastValue(accessToken).ToString();
        }

        private void Collect()
        {
            string accessToken = Session[ACC_KEY] as string;
            Triple7.PlayerCollect(accessToken);
            int status = Triple7.GetGameStatus(accessToken);
            if (status == 4)
            {
                btnCollect.Enabled = btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = false;
                txtBet.Enabled = btnBet.Enabled = true;
                SetInfo(null);
            }
        }

        protected void btnBet_Click(object sender, EventArgs e)
        {
            try
            {
                string accessToken = Session[ACC_KEY] as string;
                int au = int.Parse(txtBet.Text);
                try
                {
                    Triple7.PlayerBetAu(au, accessToken);
                    SetInfo(Triple7.GetRateConfig(au, accessToken));
                    btnCol1Hi.Enabled = btnCol1Lo.Enabled = btnCol2Hi.Enabled = btnCol2Lo.Enabled = btnCol3Hi.Enabled = btnCol3Lo.Enabled = true;
                    txtBet.Enabled = btnBet.Enabled = false;
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnNudges_Click(object sender, EventArgs e)
        {
            try
            {
                string accessToken = Session[ACC_KEY] as string;
                Triple7.PlayerSelectNudges(accessToken);
                Triple7SessionInfo info = Triple7.GetSessionInfo(accessToken);
                SetInfo(Triple7.GetRateConfig(info.AuCurrent, accessToken));
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }

        #region
        protected void btnCol1Hi_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(1, 1);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCol1Lo_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(1, 2);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCol2Hi_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(2, 1);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCol2Lo_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(2, 2);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCol3Hi_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(3, 1);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCol3Lo_Click(object sender, EventArgs e)
        {
            try
            {
                SelectHiLo(3, 2);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        #endregion

        protected void btnCollect_Click(object sender, EventArgs e)
        {
            try
            {
                Collect();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
    }
}